package org.peipito.prs;

import org.peipito.prs.engine.GameEngine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.SurfaceView;

// TODO Rename the class for something like UserInput ..
public class RPSWhite {
	
	private static final String TAG = RPSWhite.class.getSimpleName();
	
	public static int ROCK 		= 0;
	public static int PAPER 	= 1;
	public static int SCISSOR 	= 2;
	
	/**
	 * contain the game logic.
	 */
	private GameEngine gameEngine;
	
	private Bitmap bitmapRock; 			// The white paper rock scissor bitmap
	private Bitmap bitmapPaper; 		// The white paper rock scissor bitmap
	private Bitmap bitmapScissor; 		// The white paper rock scissor bitmap
	private Bitmap bitmapMenuDown;		// the menu down bitmap.
	
	private Bitmap bitmapBG;
	
	private Rect sourceRect; 			// the rectangle to be drawn from the animation bitmap
	private Rect bgRect;				// the background rectangle
	
	private int spriteWidth; 			// the width of the sprite
	private int spriteHeight;			// the height of the sprite
	
	private int x;						// the X coordinate of the object (top left of the image)
	private int y;						// the Y coordinate of the object (top left of the image)
	
	/**
	 * Bitmap that details the 5 seconds delay before that the game start.
	 * 
	 */
	private Bitmap [] countDowns;
	private int countDown;
	
	public RPSWhite(Bitmap bitmapRock,
			Bitmap bitmapPaper,
			Bitmap bitmapScissor,
			Bitmap bitmapMenuDown,
			int x, int y) {
		super();
		this.bitmapPaper 	= bitmapPaper;
		this.bitmapRock 	= bitmapRock;
		this.bitmapScissor 	= bitmapScissor;
		this.bitmapMenuDown = bitmapMenuDown;
		
		this.spriteWidth = bitmapRock.getWidth(); // there is three images in the bitmap.
		this.spriteHeight = bitmapRock.getHeight();
		
		sourceRect = new Rect(0,0,spriteWidth,spriteHeight);
		
		this.x = x;
		this.y = y;
	}

	public void init() {
//		countDown = 5;
		started = false;
		choose(0); // 0 is for paper.
		gameEngine.play(1);	// 1 is for paper 
		
	}
	
	private boolean started = false;
	private long previousGameTime;
	
	/**
	 * 
	 * @param gameTime
	 */
	public void update(long gameTime) {
		
		if(!started){
			previousGameTime = gameTime;
			started = true;
			return;
		}
		
	}
	
	// the draw method which draws the corresponding frame
	public void draw(Canvas canvas,SurfaceView surfaceView) {
		// where to draw the sprite
		
		
		int menuDownTop = surfaceView.getBottom()  - bitmapMenuDown.getHeight();
		canvas.drawBitmap(bitmapMenuDown, 0, menuDownTop, null);
		
		Paint paint = new Paint();
		paint.setARGB(50, 0, 255, 0);
		
//		Rect dest = new Rect(0,											// left 
//				surfaceView.getHeight()/2, 								// top
//				surfaceView.getWidth(), 								// right
//				menuDownTop);											// bottom
//		
//		Rect computer = new Rect(0,										// left 
//				0, 														// top
//				surfaceView.getWidth(), 								// right
//				surfaceView.getHeight() / 2);							// bottom
		
		if (rock){				// Rock has been choose by the user.
			Rect rect = new Rect(bitmapMenuDown.getWidth() / 3,			// left  
					menuDownTop, 										// top
					bitmapMenuDown.getWidth()* 2 / 3, 					// right
					surfaceView.getBottom());							// bottom
			canvas.drawRect(rect,paint);
			
//			canvas.drawBitmap(bitmapRock, sourceRect, dest, null);
			canvas.drawBitmap(bitmapRock, 0, 380, null);			
		}else if (scissor){		// Scissor has been choose by the user.
			Rect rect = new Rect(bitmapMenuDown.getWidth()* 2 / 3,		// left  
					menuDownTop, 										// top
					bitmapMenuDown.getWidth(),	 						// right
					surfaceView.getBottom());							// bottom
			canvas.drawRect(rect,paint);
			
//			canvas.drawBitmap(bitmapScissor, sourceRect, dest, null);
			canvas.drawBitmap(bitmapScissor, 0, 380, null);
		} else { 				// by default if will be paper .. 
			Rect rect = new Rect(0,										// left  
					menuDownTop, 										// top
					bitmapMenuDown.getWidth() / 3, 						// right
					surfaceView.getBottom());							// bottom
			canvas.drawRect(rect, paint);
			
//			canvas.drawBitmap(bitmapPaper, sourceRect, dest, null);
			canvas.drawBitmap(bitmapPaper, 0, 380, null);
		}
		
	}
	
	private boolean rock 		= false;
	private boolean paper 		= false;
	private boolean scissor 	= false;
	
	/**
	 * Action to be done when the screen has been touched.
	 * 
	 * @param x coordinate of the touch action
	 * @param y coordinate of the touch action
	 */
	public void handleActionDown(int x, int y,SurfaceView surfaceView) {
		
		if(gameEngine.gamePhase == GameEngine.RESULT)
			return;
		
		int top = surfaceView.getBottom()  - bitmapMenuDown.getHeight();
		
		if (y<top){
//			do nothing ...
			return;
		}else if(x<surfaceView.getWidth() / 3){
			choose(0); // 0 is for paper.
			gameEngine.play(1);	// 1 is for paper 
		}else if(x > surfaceView.getWidth() * 2 / 3){
			choose(2); // 2 is  for scissor.
			gameEngine.play(2);
		}else{
			choose(1); // 1 is for rock.
			gameEngine.play(0);
		}
	}

	/**
	 * TODO change that to (rock,0) (paper,1) (scissor,2).
	 * paper 	= 0
	 * rock		= 1
	 * scissor	= 2
	 * @param i
	 */
	private void choose(int i){
		switch(i){
		case 0 :
			rock 	= false;
			paper	= true;
			scissor = false;
			break;
		case 1:
			rock 	= true;
			paper	= false;
			scissor = false;
			break;
		case 2:
			rock 	= false;
			paper	= false;
			scissor = true;
			break;
		default:
			rock 	= false;
			paper 	= false;
			scissor = false;
		}
	}
	
	public Rect getSourceRect() {
		return sourceRect;
	}

	public void setSourceRect(Rect sourceRect) {
		this.sourceRect = sourceRect;
	}

	public int getSpriteWidth() {
		return spriteWidth;
	}

	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}

	public int getSpriteHeight() {
		return spriteHeight;
	}

	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setEngine(GameEngine engine){
		this.gameEngine = engine;
	}

	
	
	
}
